package dgut.haitaobao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dgut.haitaobao.entry.Product;
import dgut.haitaobao.entry.ProductImg;
import dgut.haitaobao.entry.ProductSku;
import dgut.haitaobao.entry.ProductVo;
import dgut.haitaobao.mapper.ProductImgMapper;
import dgut.haitaobao.mapper.ProductMapper;
import dgut.haitaobao.mapper.ProductSkuMapper;
import dgut.haitaobao.service.ProductService;
import dgut.haitaobao.util.PageHelper;
import dgut.haitaobao.util.response.HttpResponse;
import dgut.haitaobao.vo.result.ProductDetailResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 黄大山
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductImgMapper productImgMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;


    @Override
    public HttpResponse getRecommendProductList() {
        List<ProductVo> recommendProductList = productMapper.getRecommendProductList();
        return HttpResponse.success(recommendProductList);
    }

    @Override
    public HttpResponse getDetailProductByProductId(String productId) {
        Product product = productMapper.getDetailProductByProductId(productId);
        ProductDetailResultVO productDetailResultVO = new ProductDetailResultVO();
        productDetailResultVO.setProduct(product);
        if (product!=null){
            List<ProductImg> productImgs = productImgMapper.getProductImgListByProductId(productId);
            productDetailResultVO.setProductImgs(productImgs);
            List<ProductSku> productSkus = productSkuMapper.getProductSkusByProductId(productId);
            productDetailResultVO.setProductSkus(productSkus);
        }

        return HttpResponse.success(productDetailResultVO);
    }

    @Override
    public HttpResponse getProductByCategoryId(Integer categoryId, Integer pageNum, Integer lineNum) {
        int start=(pageNum-1)*lineNum;
        List<ProductVo> productByCategoryId = productMapper.getProductByCategoryId(categoryId,start , lineNum);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<Product> products = productMapper.selectList(queryWrapper);
        //计算总记录数
        int count=products.size();
        //计算总页数
        int pageCount=count%lineNum==0?count%lineNum:count%lineNum+1;
        PageHelper<ProductVo> pageHelper = new PageHelper<>(count, pageCount, productByCategoryId);
        return HttpResponse.success(pageHelper);
    }

    @Override
    public HttpResponse getProductByProductName(String productName, Integer pageNum, Integer lineNum) {
        int start=(pageNum-1)*lineNum;
        List<ProductVo> products = productMapper.getProductByProductName ("%"+productName+"%",start,lineNum);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name",productName);
        List<Product> productsAll = productMapper.selectList(queryWrapper);
        //计算总记录数
        int count=productsAll.size();
        //计算总页数
        int pageCount=count%lineNum==0?count%lineNum:count%lineNum+1;
        PageHelper<ProductVo> pageHelper=new PageHelper<>(count,pageCount,products);
        return HttpResponse.success(pageHelper);
    }

}